﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Diagnostics;
using System.Threading;

namespace Microsoft.SqlCommunity.SQLRSS
{
    /// <summary>
    /// This is a simple test client which asks the WCF web service for a syndication feed containing the database changes for 
    /// a table in the AdventureWorks2008 sample database.  You need to configure that database for Change Tracking and 
    /// start the service before this client (or any other) will work.  You can use Outlook or IE as a client as well.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Press x then enter to quit.");
                while (Console.ReadLine().Length == 0)
                {
                    using (XmlReader reader = XmlReader.Create("http://localhost/SQLRSS/GetDataChanges?rssFormat=atom&connection=local&table=Production.ProductReview"))
                    {
                        SyndicationFeed feed = SyndicationFeed.Load(reader);

                        Console.WriteLine(feed.Title.Text);
                        Console.WriteLine("Items:");
                        foreach (SyndicationItem item in feed.Items)
                        {
                            Console.WriteLine("Title: {0}", item.Title.Text);
                            Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Content).Text);
                            foreach (SyndicationLink link in item.Links)
                            {
                                Console.WriteLine("Link: {0}", link.GetAbsoluteUri().ToString());
                            }
                        }
                    }
                    Thread.Sleep(5000);
                }
            }
            catch (System.Net.WebException we)
            {
                Console.WriteLine(we.ToString());
                Console.WriteLine(we.Response);
                Console.WriteLine(we.Status);
            }
        }
    }
}
